<?php 

   require "gobal.php";
   require_once "lib/WxPay.Api.php";
   require_once "lib/WxPay.JsApiPay.php";
   require_once 'payment/log.php';
   //require "payment/WxPay.JsApiPay.php";



   $tools = new JsApiPay();
   $userInfo = $tools->GetUserInfo();

   //print_r($userInfo);
   //echo $_SERVER['REQUEST_URI'];

$openid=$userInfo["openid"];	//用户的唯一标识
$nickname=$userInfo["nickname"];	//用户昵称
$sex=$userInfo["sex"];	//用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
$province=$userInfo["province"];	//用户个人资料填写的省份
$city=$userInfo["city"];	//普通用户个人资料填写的城市
$country=$userInfo["country"];	//国家，如中国为CN
$headimgurl=$userInfo["headimgurl"];	//用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空。若用户更换头像，原有头像URL将失效。
$privilege=$userInfo["privilege"];	//用户特权信息，json 数组，如微信沃卡用户为（chinaunicom）
$unionid=$userInfo["unionid"];	//只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。详见：获取用户个人信息（UnionID机制）


$sql="select * from customer where wxmp_openid='$openid'";

$customers=fetch_rows($sql);


$customer_id=0;

if(sizeof($customers)<=0){

	$sql=" INTO `customer`( `wxmp_nickname`, `wxmp_openid`, `level`, `realname`, `remark`, `wxmp_headimgurl`, `wxmp_sex`, `wxmp_country`, `wxmp_province`, `wxmp_city`, `wxmp_privilege`, `wxmp_unionid`,last_login_at) VALUES ('$nickname','$openid',1,'$nickname','','$headimgurl',$sex,'$country','$province','$city','$privilege','$unionid',NOW())";

	$customer_id=insert_row($sql);
}else{

    $sql=" `customer` SET `wxmp_nickname`='$nickname',`wxmp_headimgurl`='$headimgurl',`wxmp_sex`=$sex,`wxmp_country`='$country',`wxmp_province`='$province',`wxmp_city`='$city',`wxmp_privilege`='$privilege',`wxmp_unionid`='$unionid',last_login_at=NOW() where wxmp_openid='$openid'";
$eff=update_row($sql);
	$customer=$customers[0];
	$customer_id=$customer["id"];
}

session_start();
$_SESSION["customer_id"]=$customer_id;

$basePath=getBasePath();
header("location: $basePath/index.php");
exit;

?>